package com.youlu.campus.common.utils;

import cn.hutool.core.util.IdUtil;
import org.apache.commons.lang3.StringUtils;

import java.util.UUID;

/**
 * 表id生成工具
 *
 * @author pdjt
 */
public class IdGeneratorUtils {

    public static String generateId() {
        return UUID.randomUUID().toString().replace("-", "").toLowerCase();
    }

    public static String generateRandomNum() {
        return DateTimeUtils.getDate() + getRandNum(1, 999999);
    }


    public static String generateRecycleCode() {

        return String.format("%04d", getRandNum(1, 9999));
    }

    /**
     * 会员卡号
     * 880+时间戳+三位随机数+4位手机尾号
     *
     * @param phone
     * @return
     */
    public static String generateCardNo(String phone) {

        StringBuilder sbl = new StringBuilder();
        String s = String.format("%03d", getRandNum(1, 999));
        sbl.append("880").append(DateTimeUtils.getDate2()).append(s);
        if (StringUtils.isNotBlank(phone)) {
            String s1 = phone.substring(7, 11);
            sbl.append(s1);
        } else {
            sbl.append("0001");
        }
        return sbl.toString();

    }

    public static String generateReserveNo() {
        StringBuilder sbl = new StringBuilder();
        sbl.append("Y").append(DateTimeUtils.getDate()).append(getRandNum(1, 999999));
        return sbl.toString();
    }

    public static String generateOrderNo() {
        StringBuilder sbl = new StringBuilder();
        sbl.append("D").append(DateTimeUtils.getDate()).append(getRandNum(1, 999999));
        return sbl.toString();
    }

    /**
     * 退款单号
     *
     * @return
     */
    public static String generateRefundFlowNo() {
        StringBuilder sbl = new StringBuilder();
        sbl.append("88").append(DateTimeUtils.getDate()).append(getRandNum(1, 999999));
        return sbl.toString();
    }

    /**
     * 资金流水编号
     *
     * @return
     */
    public static String generateFundFlowNo() {
        StringBuilder sbl = new StringBuilder();
        sbl.append("F").append(DateTimeUtils.getDate()).append(getRandNum(1, 999999));
        return sbl.toString();
    }

    public static String generateBoxNo() {
        StringBuilder sbl = new StringBuilder();
        sbl.append("306").append(DateTimeUtils.getDate()).append(getRandNum(1, 999999));
        return sbl.toString();
    }

    public static String generateEmployeeNo() {
        StringBuilder sbl = new StringBuilder();
        sbl.append(getRandNum(1, 9999));
        return sbl.toString();
    }

    /**
     * 课程订单
     *
     * @return
     */
    public static String generateCourseNo() {
        StringBuffer sb = new StringBuffer();
        sb.append("WXLTKCSP").append(DateTimeUtils.getDate()).append(getRandNum(1, 99999999));
        return sb.toString();
    }

    /**
     * 竞赛订单
     *
     * @return
     */
    public static String generateContestNo() {
        StringBuffer sb = new StringBuffer();
        sb.append("CONTEST").append(DateTimeUtils.getDate()).append(getRandNum(1, 99999999));
        return sb.toString();
    }

    /**
     * 视频会员卡
     *
     * @return
     */
    public static String generateVideoNo() {
        StringBuffer sb = new StringBuffer();
        sb.append("CARD").append(DateTimeUtils.getDate()).append(getRandNum(1, 99999999));
        return sb.toString();
    }

    /**
     * 视频会员卡退款
     *
     * @return
     */
    public static String generateVideoRefundNo() {
        StringBuffer sb = new StringBuffer();
        sb.append("CARDR").append(DateTimeUtils.getDate()).append(getRandNum(1, 99999999));
        return sb.toString();
    }

    /**
     * 课程退款订单
     *
     * @return
     */
    public static String generateRCourseNo() {
        StringBuffer sb = new StringBuffer();
        sb.append("WXLTKCSPR").append(DateTimeUtils.getDate()).append(getRandNum(1, 99999999));
        return sb.toString();
    }

    /**
     * kos 订单
     *
     * @return
     */
    public static String generateKosNo() {
        StringBuffer sb = new StringBuffer();
        sb.append("WXLTKOS").append(DateTimeUtils.getDate()).append(getRandNum(1, 99999999));
        return sb.toString();
    }

    /**
     * 提现单号 订单
     *
     * @return
     */
    public static String generateWithdraw() {
        StringBuffer sb = new StringBuffer();
        sb.append("WIDR").append(DateTimeUtils.getDate()).append(getRandNum(1, 99999999));
        return sb.toString();
    }

    /**
     * 渠道提现单号 订单
     *
     * @return
     */
    public static String generateChannelWithdraw() {
        StringBuffer sb = new StringBuffer();
        sb.append("WIDRC").append(DateTimeUtils.getDate()).append(getRandNum(1, 99999999));
        return sb.toString();
    }

    /**
     * 分账单号
     *
     * @return
     */
    public static String generateSharingProfit() {
        StringBuffer sb = new StringBuffer();
        sb.append("SHO").append(DateTimeUtils.getDate()).append(getRandNum(1, 99999999));
        return sb.toString();
    }

    /**
     * @return
     */
    public static String generateRecycleNo() {
        StringBuffer sb = new StringBuffer();
        sb.append("RECYCLE").append(DateTimeUtils.getDate()).append(getRandNum(1, 99999999));
        return sb.toString();
    }

    /**
     * 高山回收单
     *
     * @return
     */
    public static String generateGSHSNo() {
        StringBuffer sb = new StringBuffer();
        sb.append("GSHS").append(DateTimeUtils.getDate()).append(getRandNum(1, 99999999));
        return sb.toString();
    }

    /**
     * 高山预购单
     *
     * @return
     */
    public static String generateGSYGNo() {
        StringBuffer sb = new StringBuffer();
        sb.append("GSYG").append(DateTimeUtils.getDate()).append(getRandNum(1, 99999999));
        return sb.toString();
    }


    public static int getRandNum(int min, int max) {
        int randNum = min + (int) (Math.random() * ((max - min) + 1));
        return randNum;
    }

    public static String schoolNo() {
        StringBuilder sbl = new StringBuilder();
        sbl.append(getRandNum(1, 9999));
        return sbl.toString();
    }

    public static String collectorNo() {
        StringBuilder sbl = new StringBuilder();
        sbl.append(getRandNum(1, 9999));
        return sbl.toString();
    }

    /**
     * 雪花算法生成id
     *
     * @return
     */
    public static long createSnowflake() {
        // 创建雪花算法生成器 生成ID
        return IdUtil.createSnowflake(1, 1).nextId();
    }

    /**
     * 前缀+雪花算法生成id
     *
     * @return
     */
    public static String createSnowflake(String prefix) {
        // 创建雪花算法生成器 生成ID
        return prefix + createSnowflake();
    }

}
